<?php

	class Department_controller extends CI_Controller{

		function __construct(){
			parent::__construct();
			$this->data['logged_email'] = $this->session->userdata('logged_email');
		}

		function index(){
                    
                    $this->session->set_userdata('stored_url', selfURL());
                    $section = "department";
        
                    // Use for breadcrumb
                    $cfer = new Cfer(array(
                        lang('txt_dashboard') => base_url('dashboard'),
                        lang('txt_department') => base_url($section))
                    );

                    $filter = array();
                    $filter['name'] = $this->input->post('name') ? $this->input->post('name') : "";
                    $filter['group'] = $this->input->post('group');
                    $filter['id_department'] = $this->input->post('id_department');
                    $filter[PAGINATION_QUERY_STRING_SEGMENT] = $this->input->get(PAGINATION_QUERY_STRING_SEGMENT);
                    
                    $department = new Department();
                    $department = Department::getPagination($filter);
                    $department->addSelect();
                    $department->addSelect("id, name, code, disabled");
                    
                    /*
                    if ($filter['name'] != "")
                        $deparment->addWhere("name LIKE '%".$filter['name']."%'");
                    */
                    $department->orderBy("name ASC");
                    $department->find();
                    $hasuser = array();
                    $this->data['department'] = $department;
                    $this->data['filter'] = $filter;

                    $array_menus = array();
                    $filter = array();
                    $filter['parent_id'] = 0;
                    $filter['type'] = 1;
                    //Menu::getMenuTree($array_menus, $filter);

                    $this->data['cfer'] = $cfer;
                    $this->data['array_menus'] = $array_menus;
                    $this->data['section'] = $section;
                    $this->data["pagination"] = $department->pagination;

                    $this->load->view('main', $this->data);
                    
		}

		// You can place some more methods code here
        function delete($id = null) {
        
        $back = base_url('department');//$this->session->userdata('stored_url') ? $this->session->userdata('stored_url') : base_url('department');
        
        $department = new Department();

        if ($id && !$department->get($id)) {
            redirect($back);
        }
        
        $department->delete();
        redirect($back);
    
    }
    
    function add() {
        
        $back = $this->session->userdata('stored_url') ? $this->session->userdata('stored_url') : base_url('user');
        $section = 'user_form';
        
        $cfer = new Cfer(array(
            lang('txt_dashboard') => base_url('dashboard'),
            lang('txt_user') => $back,
            lang('txt_add_user') => base_url('user/add/')));
        
        $act = $this->input->get_post('act');
    
        $user = new User();
        
        if ($act == ACT_SUBMIT) {
            
            $user->first_name = $this->input->post('first_name');
            $user->last_name = $this->input->post('last_name');
            $user->email = $this->input->post('email');
            $user->pass = $this->input->post('password');
            $confirm_password = $this->input->post('confirm_password');
            $user->home_phone = $this->input->post('home_phone');
            $user->work_phone = $this->input->post('work_phone');
            $user->mobile_phone = $this->input->post('mobile_phone');
            $sel_departments = $this->input->post('id_department');
            
            if ($user->validateInput($confirm_password)) {
                
                $user->pass = do_hash($user->pass);
                
                if ($user->insert()) {
                    
                    if (count($sel_departments) > 0) {
                        foreach ($sel_departments as $id_department) {
                            $user_dept = new UserDepartment();
                            $user_dept->id_user = $user->id;
                            $user_dept->id_department = $id_department;
                            $user_dept->insert();
                        }
                    }
                    
                    redirect($back);
                    
                }
                
            }
            
        }
        
        $department = Department::getList();

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuTree($array_menus, $filter);
        
        $this->data['user'] = $user;
        $this->data['department'] = $department;
        $this->data['sel_departments'] = isset($sel_departments) && is_array($sel_departments) ? $sel_departments : array();
        $this->data['array_menus'] = $array_menus;
        $this->data['cfer'] = $cfer;
        $this->data['backlink'] = $back;
        $this->data['section'] = $section;
        
        $this->load->view('main', $this->data);
        
    }
    
    function edit($id) {
        
        $back = $this->session->userdata('stored_url') ? $this->session->userdata('stored_url') : base_url('department');
        $section = 'department_form';
        
        $cfer = new Cfer(array(
            lang('txt_dashboard') => base_url('dashboard'),
            lang('txt_department') => $back,
            lang('txt_edit_department') => base_url('department/edit/'.$id)));
        
        $act = $this->input->get_post('act');
    
        $department = new Department();
        
        if (!$id)
            redirect($back);
        
        if (!$department->get($id))
            redirect($back);
        
//        
//        $sel_departments = array();
//        
//        $user_depts = new UserDepartment();
//        $user_depts->addWhere('user_department.id_user = '.$user->id);
//        $user_depts->find();
//
//        while ($user_depts->fetchNext()) {
//            $sel_departments[] = $user_depts->id_department;
//        }
        
        if ($act == ACT_SUBMIT) {
            
            $department->name = $this->input->post('name');
            $department->code = $this->input->post('code');
            $department->disabled = $this->input->post('disabled');
   
//            if ($user->validateInput()) {
//                
//                if ($user->update()) {
//                    
//                    if (count($sel_departments) > 0) {
//                        
//                        $user_dept = new UserDepartment();
//                        $user_dept->id_user = $user->id;
//                        $user_dept->delete();
//                        
//                        foreach ($sel_departments as $id_department) {
//                            $user_dept->id_user = $user->id;
//                            $user_dept->id_department = $id_department;
//                            $user_dept->insert();
//                        }
//                    }
//                    
//                    redirect($back);
//                    
//                }
//                
//            }
//            
//        }
//        
//        $department = Department::getList();

        
        
        
    }
    
    $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuTree($array_menus, $filter);
    
    //$this->data['user'] = $user;
        $this->data['department'] = $department;
        //$this->data['sel_departments'] = isset($sel_departments) && is_array($sel_departments) ? $sel_departments : array();
        $this->data['array_menus'] = $array_menus;
        $this->data['cfer'] = $cfer;
        $this->data['backlink'] = $back;
        $this->data['section'] = $section;
        
        $this->load->view('main', $this->data);
    
}
}
